package com.jinyuan.options;

import com.jinyuan.R;
import com.jinyuan.service.MobileApplication;
import com.jinyuan.view.slidingmenuview.SlidingMenuView;

import android.app.ActivityGroup;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

/**
 * SlidingMenu控制组
 * 
 * @author Azuryleaves
 * 
 */
public class SlidingMenuControlActivity extends ActivityGroup {

	// -- 退出按钮状态
	private boolean finishCount = false;
	private ProgressDialog progressDialog;
	private static SlidingMenuView slidingMenuView;
	private FrameLayout main_slidingmenuview_leftmenu_FrameLayout,
			main_slidingmenuview_body_FrameLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MobileApplication.allActivity.add(this);
		setContentView(R.layout.slidingmenu_control);
		slidingMenuView = (SlidingMenuView) findViewById(R.id.slidingmenu_control_SlidingMenuView);
		main_slidingmenuview_leftmenu_FrameLayout = (FrameLayout) slidingMenuView
				.findViewById(R.id.main_slidingmenuview_leftmenu_FrameLayout);
		main_slidingmenuview_body_FrameLayout = (FrameLayout) slidingMenuView
				.findViewById(R.id.main_slidingmenuview_body_FrameLayout);

		// -- 左菜单
		Intent leftIntent = new Intent(SlidingMenuControlActivity.this, LeftMenuActivity.class);
		View leftView = getLocalActivityManager().startActivity(
				LeftMenuActivity.class.getName(), leftIntent).getDecorView();
		main_slidingmenuview_leftmenu_FrameLayout.removeAllViews();
		main_slidingmenuview_leftmenu_FrameLayout.addView(leftView);

		// -- 中页面
		Intent intent = new Intent(SlidingMenuControlActivity.this, MainActivity.class);
		intent.putExtras(getIntent());
		View v = getLocalActivityManager().startActivity(
				MainActivity.class.getName(), intent).getDecorView();
		main_slidingmenuview_body_FrameLayout.removeAllViews();
		main_slidingmenuview_body_FrameLayout.addView(v);
		
	}
	
	public static int getCurrentScreen() {
		return slidingMenuView.getCurrentScreen();
	}

	public static void hideMenu() {
		slidingMenuView.scrollRight();
	}

	public static void showMenu() {
		slidingMenuView.scrollLeft();
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			MainActivity centerActivity = (MainActivity) getLocalActivityManager().getActivity(MainActivity.class.getName());
			int backStackCount = centerActivity.getSupportFragmentManager().getBackStackEntryCount();
//			System.out.println("------SlidingMenu控制组------backStackCount------" + backStackCount);
			if (backStackCount > 1) {
				return centerActivity.onBackKeyDown(backStackCount, event);
			} else {
				if (event.getRepeatCount() == 0) {
					if (finishCount) {
						progressDialog = ProgressDialog.show(this,
								"请稍等片刻...", "正在为您保存状态", true, true);
						new Thread() {
							public void run() {
								progressDialog.dismiss();
								// finish();
								MobileApplication.exitApp(SlidingMenuControlActivity.this);
								interrupt();
							}
						}.start();
					} else {
						finishCount = true;
						Toast.makeText(this, "再按一次返回键退出",
								Toast.LENGTH_SHORT).show();
						new Handler().postDelayed(new Runnable() {
							@Override
							public void run() {
								finishCount = false;
							}
						}, 2000);
					}
					return true;
				} else {
					return super.dispatchKeyEvent(event);
				}
			}
		} else {
			return super.dispatchKeyEvent(event);
		}
	}

}
